package cn.ichiva.aspect;

import cn.ichiva.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Log 日志切面,用于输出执行时间
 */
@Slf4j
@Aspect
@Component
public class ExecutionTimeLogAspect {

    @Pointcut("@annotation(cn.ichiva.annotation.Log)")
    private void logPointCut() {

    }

    @Around("logPointCut()")
    private Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        String value = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(Log.class).value();
        log.info("time = {};value = {}",time,value);
        return result;
    }
}
